import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:travel/routes/index.dart';
import 'package:travel/screen/flutter_screenutil.dart';
import 'package:travel/utils/colors.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  final List<Permission> needPermissionList = [
    Permission.location,
    Permission.storage,
    Permission.notification
  ];

  @override
  void initState() {
    // Future.delayed(const Duration(seconds: 5), checkPermissions);
    super.initState();
  }

  // @override
  // void reassemble() {
  //   checkPermissions();
  //   super.reassemble();
  // }

  @override
  Widget build(BuildContext context) {
    if (GetPlatform.isAndroid || GetPlatform.isIOS) {
      SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
        statusBarIconBrightness: Brightness.light,
        statusBarColor: Colors.transparent,
      ));
    }
    return ScreenUtilInit(
      minTextAdapt: true,
      splitScreenMode: true,
      designSize: const Size(375, 750),
      builder: (_, child) {
        return GetMaterialApp(
          title: '旅游社交',
          debugShowCheckedModeBanner: false,
          locale: const Locale('zh', 'CN'),
          fallbackLocale: const Locale('zh', 'CN'),
          supportedLocales: const [Locale('zh', 'CN'), Locale('en', 'US')],
          localizationsDelegates: const [
            RefreshLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          transitionDuration: const Duration(milliseconds: 500),
          defaultTransition: Transition.cupertino,
          theme: ThemeData(
              appBarTheme: AppBarTheme(
                backgroundColor: Colors.white,
                centerTitle: true,
                titleSpacing: 0,
                iconTheme: IconThemeData(
                    color: ColorsUtils.parseColorFromHexString(
                        ColorsUtils.color3333),
                    size: 20.w,
                    weight: 700),
                systemOverlayStyle: SystemUiOverlayStyle.dark,

                /// 去掉底部阴影
                elevation: 0,

                /// 关闭滚动渐变
                scrolledUnderElevation: 0,
                surfaceTintColor: Colors.transparent,
              ),
              useMaterial3: true,
              fontFamily: 'YaHei',
              colorScheme: ColorScheme.light(
                  surface: ColorsUtils.parseColorFromHexString(
                      ColorsUtils.colorF7f7)),
              progressIndicatorTheme: ProgressIndicatorThemeData(
                  color: ColorsUtils.parseColorFromHexString(
                      ColorsUtils.color3478))),
          initialRoute: RoutePath.home,
          getPages: RoutePath.pages,
        );
      },
    );
  }

  /// 权限检查
  void checkPermissions() async {
    Map<Permission, PermissionStatus> statuses =
        await needPermissionList.request();
    statuses.forEach((key, value) {
      debugPrint('$key permissionStatus is $value');
      if (Permission.notification == key && value.isDenied) {
        openAppSettings();
        // Permission.notification.request();
      }
    });
  }
}
